package com.neu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neu.entity.FpFatherDemand;
import com.neu.entity.FpSecondDemand;
import com.neu.mapper.FpSecondDemandMapper;

import com.neu.mapper.FpFatherDemandMapper;
import com.neu.service.FpFatherDemandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class FpFatherDemandServiceImpl extends ServiceImpl<FpFatherDemandMapper, FpFatherDemand>
        implements FpFatherDemandService {
    @Autowired
    private  FpFatherDemandMapper fpFatherDemandMapper;
//    @Autowired
//    private  FpSecondDemandService fpSecondDemandService;
    @Autowired
    private  FpSecondDemandMapper fpSecondDemandMapper;



    //按照一级需求名称查询需求
    @Override
    public List<Map<String, Object>> searchByPnameWithSecondDemands(String pname) {
        // 查询符合 pname 条件的一级需求
        LambdaQueryWrapper<FpFatherDemand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(pname), FpFatherDemand::getPname, pname);
        List<FpFatherDemand> fatherDemands = this.list(queryWrapper);

        // 构造结果集
        List<Map<String, Object>> result = new ArrayList<>();

        for (FpFatherDemand fatherDemand : fatherDemands) {
            Map<String, Object> map = new HashMap<>();
            map.put("fatherDemand", fatherDemand);

            // 查询与当前一级需求关联的二级需求
            LambdaQueryWrapper<FpSecondDemand> secondDemandQuery = new LambdaQueryWrapper<>();
            secondDemandQuery.eq(FpSecondDemand::getFid, fatherDemand.getId());
            List<FpSecondDemand> relatedSecondDemands = fpSecondDemandMapper.selectList(secondDemandQuery);

            // 将关联的二级需求添加到结果中
            map.put("secondDemands", relatedSecondDemands);

            result.add(map);
        }

        return result;
    }



    @Override
    public List<Map<String, Object>> listWithSecondDemands() {
        // 查询所有一级需求
        List<FpFatherDemand> fatherDemands = this.list();

        // 构造结果集
        List<Map<String, Object>> result = new ArrayList<>();

        for (FpFatherDemand fatherDemand : fatherDemands) {
            Map<String, Object> map = new HashMap<>();
            map.put("fatherDemand", fatherDemand);

            // 使用 LambdaQueryWrapper 筛选当前一级需求关联的二级需求
            LambdaQueryWrapper<FpSecondDemand> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(FpSecondDemand::getFid, fatherDemand.getId());
            List<FpSecondDemand> relatedSecondDemands = fpSecondDemandMapper.selectList(queryWrapper);

            // 将结果加入到当前一级需求的结果集中
            map.put("secondDemands", relatedSecondDemands);

            result.add(map);
        }

        return result;
    }

    @Override
    public boolean updateByPname(FpFatherDemand fpFatherDemand) {
        // 根据 pname 查询数据库中的记录
        FpFatherDemand existingRecord = fpFatherDemandMapper.selectOne(
                new QueryWrapper<FpFatherDemand>().eq("pname", fpFatherDemand.getPname())
        );

        if (existingRecord != null) {
            // 更新记录
            existingRecord.setTaskId(fpFatherDemand.getTaskId()); // 假设需要更新 taskId
            // 添加其他需要更新的字段
            return fpFatherDemandMapper.updateById(existingRecord) > 0;
        }

        return false;
    }

}
